package com.aftersale.demo.controller;

import com.aftersale.demo.entity.*;
import com.aftersale.demo.service.*;
import com.aftersale.demo.utils.ValidGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

/**
 * @author 空
 * @date 2023/10/30 10:07
 * @desc
 */
@Api(tags="系统管理界面")
@RestController
@RequestMapping("/system")
public class SystemController {
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    UserService userService;
    @Autowired
    RolePermissionService rolePermissionService;
    @Autowired
    LogService logService;
    @Autowired
    NoticeService noticeService;


    @GetMapping("/getpagepermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "权限菜单分页查看接口")
    public Result getPagePermission(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize){
        return permissionService.getPermission(pageNum,pageSize);
    }
    @GetMapping("/getpermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "权限菜单查看接口")
    public Result getPermission(){
         return permissionService.getPermission();
    }

    @GetMapping("/selectpermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "权限菜单搜索接口")
    public Result selectPermission( @RequestParam String perName,
                                    @RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize){
        return permissionService.selectPermission(perName,pageNum,pageSize);
    }

    @DbLog(operation = "权限",type = LogType.ADD)
    @PostMapping("/addpermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "权限菜单添加接口")
    public Result addPermission(@RequestBody @Validated({ValidGroup.PerName.class,ValidGroup.PerDesc.class})  Permission permission){

        return permissionService.addPermission(permission);
    }

    @DbLog(operation = "权限",type = LogType.DELETE)
    @DeleteMapping("/deletepermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "权限菜单删除接口")
    public Result deletePermission(@RequestParam("id") int id){

        return permissionService.deletePermission(id);
    }

    @DbLog(operation = "权限",type = LogType.UPDATE)
    @PostMapping("/updatepermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "权限菜单修改接口")
    public Result updatePermission(@RequestBody @Validated({ValidGroup.PerName.class,ValidGroup.PerDesc.class})  Permission permission){

        return permissionService.updatePermission(permission);
    }

    @GetMapping("/getuser")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "用户查看接口")
    public Result getUser(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize){
        return userService.getUser(pageNum,pageSize);
    }

    @GetMapping("/selectuser")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "用户搜索接口")
    public Result selectUser(@RequestParam @NotBlank(message = "用户名不能为空")
                                 @Size(max = 20,min = 1,message = "用户名长度不能超过20位")String userName,
                            @RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        return userService.selectUser(userName,pageNum,pageSize);
    }



    @DbLog(operation = "用户",type = LogType.ADD)
    @PostMapping("/adduser")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "用户添加接口")
    public Result addUser(@RequestBody @Validated({ValidGroup.UserName.class,ValidGroup.PassWord.class,ValidGroup.Email.class}) User user){

        return userService.addUser(user);
    }

    @DbLog(operation = "用户",type = LogType.DELETE)
    @DeleteMapping("/deleteuser")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "用户删除接口")
    public Result deleteUser(@RequestParam int id){
        return userService.deleteUser(id);
    }

    @DbLog(operation = "用户",type = LogType.UPDATE)
    @PostMapping("/updateuser")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "用户修改接口")
    public Result updateUser(@RequestBody @Validated({ValidGroup.UserName.class,ValidGroup.Email.class})  User user){

        return userService.updateUser(user);
    }

    @GetMapping("/getuserproject")
    @PreAuthorize("hasAuthority('user_project')")
    @ApiOperation(value = "用户已有项目查看接口")
    public Result getUserProject(@RequestParam @NotBlank(message = "用户id不能为空") int userId,
                                 @RequestParam @NotBlank(message = "分页数据不能为空")Integer pageNum,
                                 @RequestParam @NotBlank(message = "分页数据不能为空")Integer pageSize){
        return userService.getUserProject(userId,pageNum,pageSize);
    }

    @GetMapping("/selectuserproject")
    @PreAuthorize("hasAuthority('user_project')")
    @ApiOperation(value = "用户已有项目搜索接口")
    public Result selectUserProject(@RequestParam @NotBlank(message = "用户id不能为空") int userId,
                                    @RequestParam @NotBlank(message = "项目名不能为空") String name,
                                 @RequestParam @NotBlank(message = "分页数据不能为空")Integer pageNum,
                                 @RequestParam @NotBlank(message = "分页数据不能为空")Integer pageSize){
        return userService.selectUserProject(name,userId,pageNum,pageSize);
    }

    @DbLog(operation = "用户拥有的项目",type = LogType.DELETE)
    @DeleteMapping("/deleteuserproject")
    @PreAuthorize("hasAuthority('user_project')")
    @ApiOperation(value = "用户已有项目删除接口")
    public Result deleteUserProject(@RequestParam @NotBlank(message = "用户id不能为空") int userId,
                                    @RequestParam @NotBlank(message = "项目id不能为空") int projectId){
        return userService.deleteUserProject(userId,projectId);
    }

    @DbLog(operation = "用户拥有的项目",type = LogType.ADD)
    @GetMapping("/adduserproject")
    @PreAuthorize("hasAuthority('user_project')")
    @ApiOperation(value = "为用户绑定项目接口")
    public Result addUserProject(@RequestParam @NotBlank(message = "用户id不能为空") int userId,
                                 @RequestParam @NotBlank(message = "项目id不能为空") int projectId){
        return userService.addUserProject(userId,projectId);
    }

    @GetMapping("/getrole")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "角色单查看接口")
    public Result getRole(){
        return roleService.getRole();
    }

    @GetMapping("/getrolepermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "角色权限菜单查看接口")
    public Result getRolePermission(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize){
        return roleService.getRolePermission(pageNum,pageSize);
    }

    @GetMapping("/selectrolepermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "角色权限菜单搜索接口")
    public Result selectRolePermission(@RequestParam String roleName,
                                    @RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize){
        return roleService.selectRolePermission(roleName,pageNum,pageSize);
    }

    @DbLog(operation = "角色权限",type = LogType.UPDATE)
    @PostMapping("/managerolepermission")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "角色权限配置接口")
    public Result manageRolePermission(@RequestBody  Role role){
        return rolePermissionService.manageRolePermission(role);
    }

    @DbLog(operation = "角色",type = LogType.ADD)
    @PostMapping("/addrole")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "角色添加接口")
    public Result addRole(@RequestBody @Validated Role role){
        return roleService.addRole(role);
    }

    @DbLog(operation = "角色",type = LogType.DELETE)
    @DeleteMapping("/deleterole")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "角色删除接口")
    public Result deleteRole(@RequestParam int id){
        return roleService.deleteRole(id);
    }

    @DbLog(operation = "角色",type = LogType.UPDATE)
    @PostMapping("/updaterole")
    @PreAuthorize("hasAuthority('user_manage')")
    @ApiOperation(value = "角色修改接口")
    public Result updateRole(@RequestBody @Validated Role role){
        return roleService.updateRole(role);
    }

    @GetMapping("/getlog")
    @PreAuthorize("hasAuthority('log')")
    @ApiOperation(value = "日志查看接口")
    public Result getLog(@RequestParam Integer pageNum,
                         @RequestParam Integer pageSize){
        return logService.getLog(pageNum,pageSize);
    }

    @GetMapping("/selectlog")
    @PreAuthorize("hasAuthority('log')")
    @ApiOperation(value = "日志搜索接口")
    public Result selectLog(@RequestParam String operation,
                            @RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        return logService.selectLog(operation,pageNum,pageSize);
    }

    @DbLog(operation = "日志",type = LogType.ADD)
    @PostMapping("/addlog")
    @PreAuthorize("hasAuthority('log')")
    @ApiOperation(value = "日志添加接口")
    public Result addLog(@RequestBody @Validated Log log){
        return logService.addLog(log);
    }

    @DbLog(operation = "日志",type = LogType.DELETE)
    @DeleteMapping("/deletelog")
    @PreAuthorize("hasAuthority('log')")
    @ApiOperation(value = "日志删除接口")
    public Result deleteLog(@RequestParam int id){
        return logService.deleteLog(id);
    }

    @DbLog(operation = "日志",type = LogType.UPDATE)
    @PutMapping("/updatelog")
    @PreAuthorize("hasAuthority('log')")
    @ApiOperation(value = "日志修改接口")
    public Result updateLog(@RequestBody @Validated Log log){
        return logService.updateLog(log);
    }

    @GetMapping("/getnotice")
    @ApiOperation(value = "公告查看接口")
    public Result getNotice(@RequestParam Integer pageNum,
                         @RequestParam Integer pageSize){
        return noticeService.getNotice(pageNum,pageSize);
    }

    @GetMapping("/selectnotice")
    @ApiOperation(value = "公告查询接口")
    public Result getNotice(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam String title){
        return noticeService.selectNotice(pageNum,pageSize,title);
    }

    @DbLog(operation = "公告",type = LogType.ADD)
    @PostMapping("/addnotice")
    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation(value = "公告添加接口")
    public Result addNotice(@RequestBody @Validated Notice notice){
        return noticeService.addNotice(notice);
    }

    @DbLog(operation = "公告",type = LogType.DELETE)
    @DeleteMapping("/deletenotice")
    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation(value = "公告删除接口")
    public Result deleteNotice(@RequestParam int id){
        return noticeService.deleteNotice(id);
    }

    @DbLog(operation = "公告",type = LogType.UPDATE)
    @PutMapping("/updatenotice")
    @PreAuthorize("hasAuthority('notice')")
    @ApiOperation(value = "公告修改接口")
    public Result updateNotice(@RequestBody @Validated Notice notice){
        return noticeService.updateNotice(notice);
    }

}
